var myDBFile="pfe.sqlite";

var $sqlite = {
	storageService: [],
	mDBConn: [],
	
	_initService : function(file){
		//http://xulfr.org/wiki/RessourcesLibs/LectureFichierCodeAvecCommentaires
    	netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var db = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
		db.append(file);
		this.storageService[file] = Components.classes["@mozilla.org/storage/service;1"].getService(Components.interfaces.mozIStorageService);
		this.mDBConn[file] = (this.storageService[file]).openDatabase(db);
			
	},
	
	select : function(file,sql,param){
		if (this.storageService[file]== undefined){
                    this._initService(file);
		}
		var ourTransaction = false;
		if ((this.mDBConn[file]).transactionInProgress){
			ourTransaction = true;
			(this.mDBConn[file]).beginTransactionAs((this.mDBConn[file]).TRANSACTION_DEFERRED);
		}
		var statement = (this.mDBConn[file]).createStatement(sql);
                if (param){
			for (var m=2, arg=null; arg=arguments[m]; m++) {
				statement.bindUTF8StringParameter(m-2, arg);
			}
		}
		try{
			var dataset = [];
			while (statement.executeStep()){
				var row = [];
				for(var i=0,k=statement.columnCount; i<k; i++){
					row[statement.getColumnName(i)] = statement.getUTF8String(i);
				}
				dataset.push(row);
			}
			// return dataset;	
		}
		finally {
			statement.reset();
		}
		if (ourTransaction){
			(this.mDBConn[file]).commitTransaction();
		}
        return dataset;	
	},
	
	
	cmd : function(file,sql,param){
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		if (this.storageService[file] == undefined){
	                    this._initService(file);
		}
		var ourTransaction = false;
		
		if ((this.mDBConn[file]).transactionInProgress){
			ourTransaction = true;
			(this.mDBConn[file]).beginTransactionAs((this.mDBConn[file]).TRANSACTION_DEFERRED);
		}
		var statement = (this.mDBConn[file]).createStatement(sql);
		if (param){
			for (var m=2, arg=null; arg=arguments[m]; m++) {
				statement.bindUTF8StringParameter(m-2, arg);
			}
		}
		try{
			statement.execute();
		}
		finally {
			statement.reset();
		}
		if (ourTransaction){
			(this.mDBConn[file]).commitTransaction();
		}
	}	

}

function parcourir(){
        try{
                netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
                var nsIFilePicker = Components.interfaces.nsIFilePicker;
                var fp = Components.classes["@mozilla.org/filepicker;1"]
                                  .createInstance(nsIFilePicker);
                fp.init(window, "Select a File", nsIFilePicker.modeOpen);
                fp.appendFilter("Fichier Images","*.jpg;*.jpeg;*.bmp;*.png; ");
                
                var res = fp.show();
                if (res == nsIFilePicker.returnOK){
                        var fichier = fp.file;
                         var chemin = fichier.path; 
                        document.getElementById("image_prod").setAttribute("value",chemin);
                        alert ('chemin = '+chemin);
                }


                
        }catch(ex2){ alert("interface:parcourir: "+ex2); }
} 


function OuvrirLien(lien){
	window.location.href =lien;
	return(lien);
}



function boucle_select(statement){
		var dataset = [];
		while (statement.executeStep()){
			var row = [];
			for(var i=0,k=statement.columnCount; i<k; i++){
				row[statement.getColumnName(i)] = statement.getUTF8String(i);
			}
			dataset.push(row);
		}
		return dataset;
	
}
function connect_DB(){
	netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
	var file = Components.classes["@mozilla.org/file/directory_service;1"]
	                     .getService(Components.interfaces.nsIProperties)
	                     .get("ProfD", Components.interfaces.nsIFile);
		file.append(myDBFile);
	
		var storageService = Components.classes["@mozilla.org/storage/service;1"]
	                        .getService(Components.interfaces.mozIStorageService);
		var mDBConn = storageService.openDatabase(file);
		return mDBConn;
}

function RC(chaine, quoi, par)
{
	i=0;
	k=0;
	r="";
	match=false;
	while(i<chaine.length)
	{
		c=chaine.charAt(i);
		if(c==quoi.charAt(k))
		{
			match=true;k++;
		} 
		else 
		{	
			if(match==true)
				{
					for(z=i-k; z<i; z++)
					{
						r=r+chaine.charAt(z);
					}
				}
		match=false;
		k=0;};
		if(match==false)
		{
			r=r+c;
			i++;
		}
		else
		{
			if(k==quoi.length)
			{
				r=r+par;
				match=false;
				k=0;
			}
			i++;
		}
	}
	return r;
}

function inserer_produit (){
	try {		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		desc_prod = document.getElementById("desc_prod").value;
		prix_prod = document.getElementById("prix_prod").value;
		image_prod= document.getElementById("image_prod").value;
		quantite_prod= document.getElementById("quantite_prod").value;
		var mDBConn = connect_DB();
		var sql = 'INSERT INTO produit(prix_produit,description_produit,image,quantite_prod) VALUES(?1,?2,?3,?4);';
		var statement = mDBConn.createStatement(sql);
	    statement.bindUTF8StringParameter(0, prix_prod);
	    statement.bindUTF8StringParameter(1, desc_prod);
	    statement.bindUTF8StringParameter(2, image_prod);
	    statement.bindUTF8StringParameter(3, quantite_prod);
	    statement.execute();
	    statement.reset();	
	    alert("Le produit a ete ajouter avec succes");
	    OuvrirLien(lien);
	}
	catch(ex2){
		alert("interface: inserer_produit: "+ex2);
	}
}

function inserer_client (){
	try {		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		nom_client = document.getElementById("nom_client").value;
	    prenom_client = document.getElementById("prenom_client").value;
		adresse_client= document.getElementById("adresse_client").value;
		cin= document.getElementById("cin").value;
		telephone= document.getElementById("telephone").value;
		var mDBConn = connect_DB();
		var sql = 'INSERT INTO client(nom_client , prenom_client , adresse_client, cin , telephone) VALUES(?1,?2,?3,?4,?5);';
		var statement = mDBConn.createStatement(sql);
	    statement.bindUTF8StringParameter(0, nom_client);
	    statement.bindUTF8StringParameter(1, prenom_client);
	    statement.bindUTF8StringParameter(2, adresse_client);
	    statement.bindUTF8StringParameter(3, cin);
	    statement.bindUTF8StringParameter(4, telephone);
	    statement.execute();
	    statement.reset();	
	}
	catch(ex2){
		alert("interface: inserer_client: "+ex2);
	}
}

function inserer_fournisseur (){
	
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
	    adresse_frns = document.getElementById("adresse_frns").value;
	    tel_frns = document.getElementById("tel_frns").value;
		nom_frns= document.getElementById("nom_frns").value;
		alert("hello "+tel_frns+" "+nom_frns);
		var mDBConn = connect_DB();
		var sql = 'INSERT INTO fournisseur(adresse_frns, tel_frns, nom_frns) VALUES(?1,?2,?3);';
		var statement = mDBConn.createStatement(sql);
	    statement.bindUTF8StringParameter(0, adresse_frns);
	    statement.bindUTF8StringParameter(1, tel_frns);
	    statement.bindUTF8StringParameter(2, nom_frns);
	    
	    statement.execute();
	    statement.reset();	

}

function insert_user (){
	try {		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		login = document.getElementById("login_i").value;
		password = document.getElementById("password_i").value;
		type_user=document.getElementById("type_user").value;
		alert(type_user+" "+login+"  : "+password);
		var mDBConn = connect_DB();
		var sql = 'INSERT INTO utilisateur(login,pwd,type_user) VALUES(?1,?2,?3);';
		var statement = mDBConn.createStatement(sql);
	    statement.bindUTF8StringParameter(0, login);
	    statement.bindUTF8StringParameter(1, password);
	    statement.bindUTF8StringParameter(2, type_user);
	    statement.execute();
	    statement.reset();	    
	    //Partie login
		alert("Utilisateur : "+login+" est ajoute avec succes"); 
	}
	catch(ex2){
		alert("interface: insert_user: "+ex2);
	}
}

function getClient(){
try{
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
		file.append(myDBFile);
		var storageService = Components.classes["@mozilla.org/storage/service;1"]
		                        .getService(Components.interfaces.mozIStorageService);
		var mDBConn = storageService.openDatabase(file);		
		var statement = mDBConn.createStatement('SELECT * from client;');
		var myArray1 = boucle_select(statement);
		j=0;
		//alert(myArray1.length);
		liste_clients=document.getElementById("liste_clients");
		hbox_client= createHbox("hbox_"+j);
		id_client=createLabel(" Code Client "); 
		id_client.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		id_client.setAttribute("width","75");
		nom_client=createLabel(" Nom ");
		nom_client.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		nom_client.setAttribute("width","120");
		prenom_client=createLabel(" Prenom ");
		prenom_client.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		prenom_client.setAttribute("width","120");
		adresse_client=createLabel(" Adresse ");
		adresse_client.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		adresse_client.setAttribute("width","300");
		cin =createLabel(" CIN ");
		cin.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		cin.setAttribute("width","75");
		telephone=createLabel(" Telephone ");
		telephone.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		telephone.setAttribute("width","75");
		hbox_client.appendChild(id_client);
		hbox_client.appendChild(nom_client);
		hbox_client.appendChild(prenom_client);
		hbox_client.appendChild(adresse_client);
		hbox_client.appendChild(cin);
		hbox_client.appendChild(telephone);
		liste_clients.appendChild(hbox_client);
		for(var j=0;j<myArray1.length;j++){
			//return myArray1[j]['client'];
			//alert("hello");
			hbox_client= createHbox("hbox_"+j);
			id_client=createLabel(" "+myArray1[j]['id_client']+" "); 
			id_client.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			id_client.setAttribute("width","75");
			nom_client=createLabel(" "+myArray1[j]['nom_client']+" ");
			nom_client.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			nom_client.setAttribute("width","120");
			prenom_client=createLabel(" "+myArray1[j]['prenom_client']+" ");
			prenom_client.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			prenom_client.setAttribute("width","120");
			adresse_client=createLabel(" "+myArray1[j]['adresse_client']+" ");
			adresse_client.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			adresse_client.setAttribute("width","300");
			cin =createLabel(" "+myArray1[j]['cin']+" ");
			cin.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			cin.setAttribute("width","75");
			telephone=createLabel(" "+myArray1[j]['telephone']+" ");
			telephone.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			telephone.setAttribute("width","75");
			sup=createLabel(" Supprimer ");
			sup.setAttribute("style","color:red;border-style:solid;text-width:41px;");
			sup.setAttribute("onclick","supprimer_client('"+myArray1[j]['id_client']+"');");
			sup.setAttribute("width","75");
			hbox_client.appendChild(id_client);
			hbox_client.appendChild(nom_client);
			hbox_client.appendChild(prenom_client);
			hbox_client.appendChild(adresse_client);
			hbox_client.appendChild(cin);
			hbox_client.appendChild(telephone);
			hbox_client.appendChild(sup);
			liste_clients.appendChild(hbox_client);
		}
		statement.reset();
	}
	catch(ex2){ 
		alert("interface:salut: "+ex2); 
		statement.reset();
		
	}
} 

function getProduit(){
try{
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
		file.append(myDBFile);
		var storageService = Components.classes["@mozilla.org/storage/service;1"]
		                        .getService(Components.interfaces.mozIStorageService);
		var mDBConn = storageService.openDatabase(file);		
		var statement = mDBConn.createStatement('SELECT * from produit;');
		var myArray1 = boucle_select(statement);
		j=0;
		//alert(myArray1.length);
		//id_produit , prix_produit money, description_produit text(300), image image, quantite_prod int);';
		liste_produits=document.getElementById("liste_produits");
		hbox_produit= createHbox("hbox_"+j);
		id_produit=createLabel(" Code produit "); 
		id_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		id_produit.setAttribute("width","90");
		prix_produit=createLabel(" Prix ");
		prix_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		prix_produit.setAttribute("width","120");
		description_produit=createLabel(" Description ");
		description_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		description_produit.setAttribute("width","300");
		//description_produit.setAttribute("flex","2");
		image=createLabel(" Image ");
		image.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		image.setAttribute("width","200");
		quantite_prod =createLabel(" Quantite ");
		quantite_prod.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		quantite_prod.setAttribute("width","75");
		suppression= createLabel(" Supprimer ");
		suppression.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		suppression.setAttribute("width","75");
		commander= createLabel(" Commander / Quantite ");
		commander.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		commander.setAttribute("width","250");
		hbox_produit.appendChild(id_produit);
		hbox_produit.appendChild(prix_produit);
		hbox_produit.appendChild(description_produit);
		hbox_produit.appendChild(quantite_prod);
		hbox_produit.appendChild(image);
		hbox_produit.appendChild(suppression);
		hbox_produit.appendChild(commander);
		liste_produits.appendChild(hbox_produit);
		for(var j=0;j<myArray1.length;j++){
			//return myArray1[j]['client'];
			//alert("hello");
			hbox_produit= createHbox("hbox_"+j);
			id_produit=createLabel(" "+myArray1[j]['id_produit']+" "); 
			id_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			id_produit.setAttribute("width","90");
			prix_produit=createLabel(" "+myArray1[j]['prix_produit']+" DT ");
			prix_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			prix_produit.setAttribute("width","120");
			hbox_desc= createHbox("hbox_");
			//hbox_desc.setAttribute("maxwidth","300");
			hbox_desc.setAttribute("width","300");
			hbox_desc.setAttribute("height","100");	
			hbox_desc.setAttribute("style","color:white;border-style:solid;text-width:15px;overflow:auto;margin-left:6px;");
			description_produit=createLabel(" "+myArray1[j]['description_produit']+" ");
			//description_produit.value=" "+myArray1[j]['description_produit']+" ";
			description_produit.setAttribute("style","color:white");
			description_produit.setAttribute("width","280");
			//description_produit.setAttribute("maxwidth","290");
			//description_produit.setAttribute("flex","2");
			image=createImage("file:///"+RC(myArray1[j]['image'],"\\","/"));
			//alert(RC(myArray1[j]['image'],"\\","/"));
			image.setAttribute("style","color:red;border-style:solid;margin-left:6px;");
			image.setAttribute("width","200");
			image.setAttribute("height","100");			
			quantite_prod =createLabel(" "+myArray1[j]['quantite_prod']+" ");
			quantite_prod.setAttribute("style","color:white;border-style:solid;text-width:41px;margin-left:11px;");
			quantite_prod.setAttribute("width","75");
			sup=createLabel(" Supprimer ");
			sup.setAttribute("style","color:red;border-style:solid;text-width:41px;background-color:white;margin-left:11px;");
			sup.setAttribute("onclick","supprimer_produit('"+myArray1[j]['id_produit']+"');");
			sup.setAttribute("width","75");
			com=createCheck(" Commander ");
			
			com.setAttribute("style","color:white;border-style:solid;text-width:41px;;");
			//com.setAttribute("onclick","commander_produit('"+myArray1[j]['id_produit']+"');");
			com.setAttribute("id",myArray1[j]['id_produit']);
			com.setAttribute("width","75");
			texte1=createText("quantite_prod"+myArray1[j]['id_produit']);
			vbox_qte=createHbox("vbox_qte_"+j);
			vbox_qte.setAttribute("align","center");
			vbox_qte.appendChild(texte1);				
			
			hbox_produit.appendChild(id_produit);
			hbox_produit.appendChild(prix_produit);
			hbox_desc.appendChild(description_produit);
			hbox_produit.appendChild(hbox_desc);
			hbox_produit.appendChild(quantite_prod);
			hbox_produit.appendChild(image);
			hbox_produit.appendChild(sup);
			hbox_produit.appendChild(com);
			hbox_produit.appendChild(vbox_qte);
			liste_produits.appendChild(hbox_produit);
			
		}
		bt_valider_cmd=createButton("bt_valider","               Valider votre Commande               ");
		bt_valider_cmd.setAttribute("onclick","valider_commande()");
		vbox_qte=createVbox("vbox_bt_");
		vbox_qte.setAttribute("align","center");
		vide=createLabel("  "); 
		vbox_qte.setAttribute("height","300");
		vbox_qte.appendChild(bt_valider_cmd);
		liste_produits.appendChild(vide);
		liste_produits.appendChild(vide.cloneNode(true));
		liste_produits.appendChild(vbox_qte);
		statement.reset();
	}
	
	catch(ex2){ 
		alert("interface:salut: "+ex2); 
		statement.reset();
		
	}
}
function supprimer_client(id_client){
try {
		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var mDBConn = connect_DB();
		alert (id_client);
		var sql = 'DELETE FROM client WHERE id_client=?1;';
		var statement = mDBConn.createStatement(sql);
	    statement.bindUTF8StringParameter(0, id_client);
	    statement.execute();
	    statement.reset();	
	    location.reload(true);
	}
	catch(ex2){
		alert("interface: supprimer_client: "+ex2);
	}
}

function supprimer_fournisseur(id_fournisseur){
try {
		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var mDBConn = connect_DB();
		alert (id_fournisseur);
		var sql = 'DELETE FROM fournisseur WHERE id_fournisseur=?1;';
		var statement = mDBConn.createStatement(sql);
	    statement.bindUTF8StringParameter(0, id_fournisseur);
	    statement.execute();
	    statement.reset();	
	    location.reload(true);
	}
	catch(ex2){
		alert("interface: supprimer_fournisseur: "+ex2);
	}
}

function supprimer_produit(id_produit){
try {
		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var mDBConn = connect_DB();
		//alert (id_fournisseur);
		var sql = 'DELETE FROM produit WHERE id_produit=?1;';
		var statement = mDBConn.createStatement(sql);
	    statement.bindUTF8StringParameter(0, id_produit);
	    statement.execute();
	    statement.reset();	
	    location.reload(true);
	}
	catch(ex2){
		alert("interface: supprimer_produit: "+ex2);
	}
}

function commander_produit(id_produit){
try {
		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var mDBConn = connect_DB();
		//alert (id_produit);
		var statement = mDBConn.createStatement('select quantite_prod from produit WHERE id_produit=?1;');
		statement.bindUTF8StringParameter(0, id_produit);
		var myArray1 = boucle_select(statement);
		qte_a=myArray1[0]['quantite_prod'];
		qte=document.getElementById("quantite_prod"+id_produit).value;
		alert(qte_a+"-"+qte+"="+(qte_a-qte));
		var sql = 'UPDATE produit SET quantite_prod = ?3-?2 WHERE id_produit=?1;';
		var statement = mDBConn.createStatement(sql);
	    statement.bindUTF8StringParameter(0, id_produit);
	    statement.bindUTF8StringParameter(1, qte);
	    statement.bindUTF8StringParameter(2, qte_a);
	    statement.execute();
	    statement.reset();	
	    location.reload(true);
	}
	catch(ex2){
		alert("interface: commander_produit: "+ex2);
	}
}

function valider_commande(){
try {
		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var mDBConn = connect_DB();
		var lien="";
		checkbox=document.getElementsByTagName("checkbox");
		for (var i=0;i<checkbox.length;i++)
		{
			if (checkbox[i].getAttribute("checked")=="true")
			{
				var statement = mDBConn.createStatement('select quantite_prod from produit WHERE id_produit=?1;');
				statement.bindUTF8StringParameter(0, checkbox[i].getAttribute("id"));
				var myArray1 = boucle_select(statement);
				qte_a=myArray1[0]['quantite_prod'];
				qte=document.getElementById("quantite_prod"+checkbox[i].getAttribute("id")).value;
				//alert(qte_a+"-"+qte+"="+(qte_a-qte));
				var sql = 'UPDATE produit SET quantite_prod = ?3-?2 WHERE id_produit=?1;';
				var statement = mDBConn.createStatement(sql);
			    statement.bindUTF8StringParameter(0, checkbox[i].getAttribute("id"));
			    statement.bindUTF8StringParameter(1, qte);
			    statement.bindUTF8StringParameter(2, qte_a);
			    statement.execute();
			    statement.reset();	
			    if (lien=="")	
			    	lien=lien+"p="+checkbox[i].getAttribute("id")+"&q="+qte;
			    else 
			    	lien=lien+"&p="+checkbox[i].getAttribute("id")+"&q="+qte;
			 }
	    }
	    OuvrirLien("chrome://pfeIsetCom/content/Facture.xul?"+lien);
	    //location.reload(true);
	}
	catch(ex2){
		alert("interface: commander_produit: "+ex2);
	}
}

function etablir_facture(){
try{
	netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
	var file = Components.classes["@mozilla.org/file/directory_service;1"]
                    .getService(Components.interfaces.nsIProperties)
                    .get("ProfD", Components.interfaces.nsIFile);
	file.append(myDBFile);
	var storageService = Components.classes["@mozilla.org/storage/service;1"]
	                        .getService(Components.interfaces.mozIStorageService);
	var mDBConn = storageService.openDatabase(file);		

	j=0;
	//alert(myArray1.length);
	//id_produit , prix_produit money, description_produit text(300), image image, quantite_prod int);';
	liste_produits=document.getElementById("liste_produits");
	hbox_produit= createHbox("hbox_"+j);
	id_produit=createLabel(" Code produit "); 
	id_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
	id_produit.setAttribute("width","90");
	prix_produit=createLabel(" Prix Unitaire");
	prix_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
	prix_produit.setAttribute("width","120");
	description_produit=createLabel(" Description ");
	description_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
	description_produit.setAttribute("width","300");
	//description_produit.setAttribute("flex","2");
	image=createLabel(" Image ");
	image.setAttribute("style","color:white;border-style:solid;text-width:41px;");
	image.setAttribute("width","50");
	quantite_prod =createLabel(" Quantite ");
	quantite_prod.setAttribute("style","color:white;border-style:solid;text-width:41px;");
	quantite_prod.setAttribute("width","75");
	commander= createLabel(" Total ");
	commander.setAttribute("style","color:white;border-style:solid;text-width:41px;");
	commander.setAttribute("width","120");
	hbox_produit.appendChild(image);
	hbox_produit.appendChild(id_produit);
	hbox_produit.appendChild(prix_produit);
	hbox_produit.appendChild(description_produit);
	hbox_produit.appendChild(quantite_prod);
	
	hbox_produit.appendChild(commander);
	liste_produits.appendChild(hbox_produit);
	var parameters = location.search.substring(1).split("&");
	var k=0;
	//alert(parameters.length);
	for(var i=0;i<parameters.length;i++){	
		var temp = parameters[i].split("=");
	    var temp2 = parameters[i+1].split("=");
	    p = unescape(temp[1]);
	    q=unescape(temp2[1]);	
	    //alert("id_produit="+p+" quantite="+q);
		var statement = mDBConn.createStatement('SELECT * from produit WHERE id_produit=?1;');
		statement.bindUTF8StringParameter(0, p);
		var myArray1 = boucle_select(statement);
		liste_produits=document.getElementById("liste_produits");
		hbox_produit= createHbox("hbox_"+j);
		id_produit=createLabel(" "+p+" "); 
		id_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;;margin-left:11px;");
		id_produit.setAttribute("width","90");
		prix_produit=createLabel(" "+myArray1[j]['prix_produit']+" DT ");
		prix_produit.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		prix_produit.setAttribute("width","120");
		hbox_desc= createHbox("hbox_");
		//hbox_desc.setAttribute("maxwidth","300");
		hbox_desc.setAttribute("width","300");
		hbox_desc.setAttribute("height","50");	
		hbox_desc.setAttribute("style","color:white;border-style:solid;text-width:15px;overflow:auto;margin-left:6px;");
		description_produit=createLabel(" "+myArray1[j]['description_produit']+" ");
		//description_produit.value=" "+myArray1[j]['description_produit']+" ";
		description_produit.setAttribute("style","color:white");
		description_produit.setAttribute("width","280");
		//description_produit.setAttribute("flex","2");
		image=createImage("file:///"+RC(myArray1[j]['image'],"\\","/"));
		image.setAttribute("style","color:white;border-style:solid;text-width:41px;;margin-left:6px;");
		image.setAttribute("width","50");
		image.setAttribute("height","50");	
		quantite_prod =createLabel(" "+q+" "); 
		quantite_prod.setAttribute("style","color:white;border-style:solid;text-width:41px;margin-left:11px;");
		quantite_prod.setAttribute("width","75");
		total_p= createLabel(q*myArray1[j]['prix_produit']);
		total_p.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		total_p.setAttribute("width","120");
		total_p.setAttribute("id","t_"+k);
		k++;
		hbox_produit.appendChild(image);
		hbox_produit.appendChild(id_produit);
		hbox_produit.appendChild(prix_produit);
		hbox_desc.appendChild(description_produit);
		hbox_produit.appendChild(hbox_desc);
		hbox_produit.appendChild(quantite_prod);
		hbox_produit.appendChild(total_p);
		liste_produits.appendChild(hbox_produit);
		i++;
	}
	mt=0;
	for(var i=0;i<parameters.length/2;i++){	
		t=parseInt(document.getElementById("t_"+i).value);
		mt=mt+t;
	}
	mt_ht =createLabel(" Montant HT     : "+mt+" DT"); 
	mt_ht.setAttribute("style","color:white;border-style:solid;text-width:41px;margin-left:610px;");
	mt_ht.setAttribute("width","206");
	mt_tax =createLabel(" Taxe                   : "+(mt*0.18)+" DT"); 
	mt_tax.setAttribute("style","color:white;border-style:solid;text-width:41px;margin-left:610px;");
	mt_tax.setAttribute("width","206");
	mt_ttc =createLabel(" Montant TTC    : "+(mt+(mt*.18))+" DT"); 
	mt_ttc.setAttribute("style","color:white;border-style:solid;text-width:41px;margin-left:610px;");
	mt_ttc.setAttribute("width","206");
	bt_valider_cmd=createButton("bt_valider","               Paiement               ");
	bt_valider_cmd.setAttribute("onclick","payer()");
	bt_imprimer_cmd=createButton("bt_imprimer_cmd","               Imprimer               ");
	bt_imprimer_cmd.setAttribute("onclick","imprimer_facture()");
	vbox_qte=createVbox("vbox_bt_");
	vbox_qte.setAttribute("align","center");
	vide=createLabel("  "); 
	hbox_bt= createHbox("hbox_bt");
	vbox_qte.setAttribute("height","300");
	vbox_qte.appendChild(mt_ht);
	vbox_qte.appendChild(mt_tax);
	vbox_qte.appendChild(mt_ttc);
	liste_produits.appendChild(vide);
	liste_produits.appendChild(vide.cloneNode(true));
	hbox_bt.appendChild(bt_valider_cmd);
	hbox_bt.appendChild(bt_imprimer_cmd);
	vbox_qte.appendChild(hbox_bt);
	//
	liste_produits.appendChild(vbox_qte);
	statement.reset();
}

catch(ex2){ 
	alert("interface:salut: "+ex2); 
	statement.reset();
	
}
}

function payer(){
	alert("Merci d'avoir utilise notre application");
	OuvrirLien('chrome://pfeIsetCom/content/accueil.xul');
}
function imprimer_facture(){
	window.print();

}
function getFournisseur(){
try{
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
		file.append(myDBFile);
		var storageService = Components.classes["@mozilla.org/storage/service;1"]
		                        .getService(Components.interfaces.mozIStorageService);
		var mDBConn = storageService.openDatabase(file);		
		var statement = mDBConn.createStatement('SELECT * from fournisseur;');
		var myArray1 = boucle_select(statement);
		j=0;
		//alert(myArray1.length);
		liste_fournisseur=document.getElementById("liste_fournisseur");
		hbox_fournisseur= createHbox("hbox_"+j);
		id_fournisseur=createLabel(" Code fournisseur "); 
		id_fournisseur.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		id_fournisseur.setAttribute("width","75");
		nom_fournisseur=createLabel(" Nom ");
		nom_fournisseur.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		nom_fournisseur.setAttribute("width","120");
		adresse_fournisseur=createLabel(" Adresse ");
		adresse_fournisseur.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		adresse_fournisseur.setAttribute("width","300");
		telephone=createLabel(" Telephone ");
		telephone.setAttribute("style","color:white;border-style:solid;text-width:41px;");
		telephone.setAttribute("width","75");
		hbox_fournisseur.appendChild(id_fournisseur);
		hbox_fournisseur.appendChild(nom_fournisseur);
		hbox_fournisseur.appendChild(adresse_fournisseur);
		hbox_fournisseur.appendChild(telephone);
		liste_fournisseur.appendChild(hbox_fournisseur);
		for(var j=0;j<myArray1.length;j++){
			//return myArray1[j]['fournisseur'];
			//alert("hello");
			hbox_fournisseur= createHbox("hbox_"+j);
			id_fournisseur=createLabel(" "+myArray1[j]['id_frns']+" "); 
			id_fournisseur.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			id_fournisseur.setAttribute("width","75");
			nom_fournisseur=createLabel(" "+myArray1[j]['nom_frns']+" ");
			nom_fournisseur.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			nom_fournisseur.setAttribute("width","120");
			adresse_fournisseur=createLabel(" "+myArray1[j]['adresse_frns']+" ");
			adresse_fournisseur.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			adresse_fournisseur.setAttribute("width","300");
			telephone=createLabel(" "+myArray1[j]['tel_frns']+" ");
			telephone.setAttribute("style","color:white;border-style:solid;text-width:41px;");
			telephone.setAttribute("width","75");
			sup=createLabel(" Supprimer ");
			sup.setAttribute("style","color:red;border-style:solid;text-width:41px;");
			sup.setAttribute("onclick","supprimer_fournisseur('"+myArray1[j]['fournisseur']+"');");
			sup.setAttribute("width","75");
			hbox_fournisseur.appendChild(id_fournisseur);
			hbox_fournisseur.appendChild(nom_fournisseur);
			hbox_fournisseur.appendChild(adresse_fournisseur);
			hbox_fournisseur.appendChild(telephone);
			hbox_fournisseur.appendChild(sup);
			liste_fournisseur.appendChild(hbox_fournisseur);
		}
		statement.reset();
	}
	catch(ex2){ 
		alert("interface:salut: "+ex2); 
		statement.reset();
		
	}
} 
function getUser(){
try{
		
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
		file.append(myDBFile);
		var storageService = Components.classes["@mozilla.org/storage/service;1"]
		                        .getService(Components.interfaces.mozIStorageService);
		var mDBConn = storageService.openDatabase(file);		
		var statement = mDBConn.createStatement('SELECT * FROM utilisateur;');

		var myArray1 = boucle_select(statement);
		j=0;
		//alert("eeeee " +myArray1[1]['login']);
		for(var j=0;j<myArray1.length;j++){
			//return myArray1[j]['login'];
			alert(myArray1[j]['login']+" : "+myArray1[j]['pwd']);
		}
		statement.reset();
	}
	catch(ex2){ 
		alert("interface:welcome: "+ex2); 
		statement.reset();
		
	}
} 


function Deconnexion(login){
	alert("Utilisateur : "+login+" est deconnecte");
	document.getElementById("vbox_user").setAttribute("hidden","true");
	document.getElementById("form_login").setAttribute("hidden","false");
	document.getElementById("login").value="";
	document.getElementById("mdp").value="";
}
function login_user(){
	try{
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

		var mDBConn = connect_DB();
		login = document.getElementById("login").value;
		password = document.getElementById("mdp").value;
		var statement = mDBConn.createStatement('SELECT id_user,login,pwd,type_user FROM utilisateur where login=?1');
		statement.bindUTF8StringParameter(0,login);
		var myArray1 = boucle_select(statement);
		test =0;
		j=0;
		for(var j=0;j<1;j++){
			if (password==myArray1[j]['pwd']) {
				alert(" Bienvenue : "+login);
				//login_frm=OuvrirLien('file:///C:/Users/samszo/workspace/pfeIsetCom/MELEK/aymen+melek/NewFile.xul');

				if (myArray1[j]['type_user']!="Administrateur")
					document.getElementById("lien_admin").setAttribute("hidden","true");
				else if (myArray1[j]['type_user']=="Administrateur")
					document.getElementById("lien_admin").setAttribute("hidden","false");
				document.getElementById("form_login").setAttribute("hidden","true");
				document.getElementById("user_id").setAttribute("value","Deconnexion : "+login);
				document.getElementById("user_id").setAttribute("onclick","Deconnexion('"+login+"')");
				document.getElementById("vbox_user").setAttribute("hidden","false");

			}
			else alert("Authentification non correcte !!!!"); ;
		}
		//if (test==0)document.getElementById("erreur_login").setAttribute("hidden","false");
		statement.reset();
	}
	catch(ex2){ 
		alert("Authentification non correcte " +ex2 ); 
		
	}
} 

function createDB(){
	try {
		var myCreateDBQuery =  'CREATE TABLE IF NOT EXISTS client (id_client INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom_client varchar(30), prenom_client varchar(30), adresse_client text(150), cin varchar(8), telephone varchar(13));';
		var myCreateDBQuery2 = 'CREATE TABLE IF NOT EXISTS fournisseur (id_frns INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, adresse_frns text(150), tel_frns varchar(13), nom_frns varchar(50));';
		var myCreateDBQuery3 = 'CREATE TABLE IF NOT EXISTS produit (id_produit INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, prix_produit money, description_produit text(300), categorie  varchar(40), image image, quantite_prod int);';
		var myCreateDBQuery4 = 'CREATE TABLE IF NOT EXISTS commander (id_client INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, id_produit uniqueidentifier NOT NULL, quantite_com image);';  
		var myCreateDBQuery5 = 'CREATE TABLE IF NOT EXISTS fournir (id_frns INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, id_produit uniqueidentifier NOT NULL, quantite_fourni int);';
		var myCreateDBQuery6 = 'CREATE TABLE IF NOT EXISTS utilisateur (id_user INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,  login varchar(40), pwd varchar(40), type_user varchar(20));';
		$sqlite._initService(myDBFile);
		$sqlite.cmd(myDBFile,myCreateDBQuery6);
		$sqlite.cmd(myDBFile,myCreateDBQuery);
		$sqlite.cmd(myDBFile,myCreateDBQuery2);
		$sqlite.cmd(myDBFile,myCreateDBQuery3);
		$sqlite.cmd(myDBFile,myCreateDBQuery4);
		$sqlite.cmd(myDBFile,myCreateDBQuery5);
		
	}
	catch(ex2){ 
		alert("interface::createDB"+ex2); 
	}
}

function createLabel(aLabel) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "label"); // create a new XUL menuitem
  item.setAttribute("value", aLabel);
  return item;
}

function createImage(aLabel) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "image");
  item.setAttribute("src", aLabel);
  return item;
}

function createCaption(aLabel) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "caption"); // create a new XUL menuitem
  item.setAttribute("label", aLabel);
  return item;
}

function createCheck(aLabel) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "checkbox"); // create a new XUL menuitem
  item.setAttribute("label", aLabel);
  return item;
}

function createButton(id,aLabel) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "button"); 
  item.setAttribute("id", id);
  item.setAttribute("label", aLabel);
  return item;
}

function createGroupbox(id) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "groupbox"); // create a new XUL menuitem
  item.setAttribute("id", id);
  return item;
}

function createText(aLabel) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "textbox"); // create a new XUL menuitem
  item.setAttribute("id", aLabel);
  return item;
}

function createHbox(aLabel) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "hbox"); // create a new XUL menuitem
  item.setAttribute("id", aLabel);
  return item;
}

function createVbox(aLabel) {
  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  var item = document.createElementNS(XUL_NS, "vbox"); // create a new XUL menuitem
  item.setAttribute("id", aLabel);
  return item;
}